;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var router = require("../hash/pageRouter");
	var validate = require("validate");
	var css_collection = require("./commodityPriceChecked.css");
	var html_add;
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化

	var init = function(num) {
		$(".table-section").removeClass("none");
		inquireBulkstockGatherList();
		inquireBulkstockCategoryPageList();
		initEvent();
		router.init();
	};

	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		$("#main").on("click", ".table-search-button-1", function() {
			pageIndex = 1;
			pageSize = 10;
			totalPageCount = 1;
			totalRecordCount = 0; //分页初始化			
			inquireBulkstockGatherList();
		});
	};

	var riseAndFallColor = function() {
		var riseAndFall = $(".riseAndFall");
		for (var i = 0, j = riseAndFall.length; i < j; i++) {
			if (riseAndFall[i].innerHTML > 0) {
				riseAndFall[i].innerHTML = "+" + riseAndFall[i].innerHTML;
			}
			if (riseAndFall[i].innerHTML.charAt(0) == "-") {
				$(riseAndFall[i]).css("color", "#33A55E");
			} else {
			}
		}
	};

	//删除商品价格汇总
	var deleteBulkstockGather = function(gatherId) {
		base.fnAjax({
			action: "disableBulkstockGather.json",
			oData: {
				gatherId: gatherId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					router.route("commodityPriceManage/commodityPriceChecked.html");
				} else {
					base.alert(2, data.message);
				}
			}
		});
	};

	//查询价格汇总列表
	var inquireBulkstockGatherList = function() {
		var body = base.parseForm($(".table-search"));
		var params = {
			pageIndex: pageIndex,
			pageSize: pageSize,
			auditState: 1
		}
		base.apply(params, body);
		base.fnAjax({
			action: "inquireBulkstockGatherList.json",
			oData: params, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#checkedTpl").html();
					var html = juicer(tpl, data);
					$("#checkedBody").html(html);

					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl($("#checked_pagenation"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireBulkstockGatherList();
						});
					$(".checkedDelete").unbind('click').click(function() {
						var $this = $(this);
						dialog.showConfirmDialog("商品价格汇总删除", "是否确认删除该条数据？", function(){
							deleteBulkstockGather($this.parents().parents().parents().attr("gatherId"));
						});
					});

					$(".checkedMonitor").unbind('click').click(function() {
						var bulkstockName = $(this).parents(".tr_style").attr("bulkstockName");
						var specification = $(this).parents(".tr_style").attr("specification");
						router.route("commodityPriceManage/commodityPriceMonitor.html?bulkstockName=" + bulkstockName + "&specification=" + specification);
					});
					riseAndFallColor();
				} else {
					base.alert(2, data.message);
				}
			}
		});
	}
	var inquireBulkstockCategoryPageList = function() {
		base.fnAjax({
			action: "inquireBulkstockCategoryPageList.json",
			oData: {
				pageIndex: 1,
				pageSize: 1000,
				activeState: true
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#categoryTpl").html();
					var html = juicer(tpl, data);
					$("#categoryId").html(html);
				} else {
					base.alert(2, data.message);
				}
			}
		});
	}
	return {
		init: init
	};
});